package com.lrkj.back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.*;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsPackageImformationService;
import com.lrkj.back.service.ZsWupinShiyongService;
import com.lrkj.back.vo.AePackageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by john on 2017/9/20.
 */
@Controller
@RequestMapping("/packageIm")
public class ShoushushiController {
    @Autowired
    private ZsPackageImformationService zsPackService;

    @Autowired
    private ZsWupinShiyongController zsWupinShiyongController;

    @Autowired
    private TbSeqService seqService;

    @Autowired
    private ZsWupinShiyongService wupinshiyongService;

    @Autowired
    private ZsPackageImformationService imformationService;



    @RequestMapping("/findImByBarcode")
    @ResponseBody
    public Map<String,Object> packageIm(String barCode){
        ZsPackageImformation zsPackageImformation = null;
        if(barCode!=null&&!"".equals(barCode)){
            zsPackageImformation= zsPackService.selectByPrimaryKey(barCode);
        }
        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("data",zsPackageImformation);
        rtnMap.put("depMap", StartUpOnLoadData.depMap);
        return rtnMap;
    }

    @RequestMapping("/findImByBarcodeAndroid")
    @ResponseBody
    public Map<String,Object> packageList(String pdata){
        ZsPackageImformation zsPackageImformation = null;
        String packageTiaoma = JSONObject.parseObject(pdata).getString("packageTiaoma");
        if(packageTiaoma!=null&&!"".equals(packageTiaoma)){
            zsPackageImformation= zsPackService.selectByPrimaryKey(packageTiaoma);
        }
        PackageAndroidYoutiaoma packageAndroid = new PackageAndroidYoutiaoma();
        packageAndroid.setPackageTiaoma(zsPackageImformation.getPackageTiaoma());
        packageAndroid.setPackageName(zsPackageImformation.getPackageName());
        String baozhuangName= "";
        String depName = "";
        String mieJunFangshi = "";
        zsPackageImformation.getPackageId();
        TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
        Integer integer = tbPackageMuban.getPackageMiejunFangshi();
        Long miejunId = 0L;
        if(integer!=null){
            miejunId = Long.valueOf(miejunId);
        }
        TbDict dict  = StartUpOnLoadData.singleDataMap.get(miejunId);
        if(dict!=null){
            mieJunFangshi = dict.getDictName();
        }
        if(tbPackageMuban!=null){
            ZsDabao zsDabao= tbPackageMuban.getZsDabao();
            if(zsDabao!=null){
                baozhuangName =  zsDabao.getDabaoName();
            }
        }
        Integer integer1 = zsPackageImformation.getKeshiId();
        String depid= "";
        if(integer!=null){
            depid = String.valueOf(integer1);
        }
        depName = StartUpOnLoadData.depMap.get(depid);
        List<PackageAndroidYoutiaoma> list = new ArrayList<>();
        if(packageAndroid!=null){
            list.add(packageAndroid);
        }
        packageAndroid.setKeshiId(depid);
        packageAndroid.setBaoZhuangName(baozhuangName);
        packageAndroid.setDepName(depName);
        packageAndroid.setMiejunFangshi(mieJunFangshi);
        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("datas",list);
        return rtnMap;
    }

    @RequestMapping("/useData")
    @ResponseBody
    public Map<String,Object> useData(String data){
        Map<String,Object> rtnMap = new HashMap<>();
        JSON json = JSONObject.parseObject(data);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMhh");
        Date date = new Date();
        try{
            Shiyong shiyong =  json.toJavaObject(Shiyong.class);
            Long seqVal = seqService.seqVal("shiyongliushui");
            ZsWupinShiyong wupinShiyong = new ZsWupinShiyong();
            ZsPackageImformation packageImformation = packageImformation = new ZsPackageImformation();
            wupinShiyong.setShiyongLiushui(sdf.format(date)+seqVal);
            wupinShiyong.setWupinbaoTiaoma(shiyong.getPackNo());
            wupinShiyong.setShiyongkeshi("15");
            wupinShiyong.setPatientId(shiyong.getPatientName());
            wupinShiyong.setPatientHospitalNo(shiyong.getHosId());
            wupinShiyong.setShoushuTemplate(shiyong.getOperationName());
            wupinShiyong.setShiyongTime(date);
//        wupinShiyong.setShoushuBianhao();
            wupinShiyong.setRecordTime(date);
            packageImformation.setPackageTiaoma(shiyong.getPackNo());
            packageImformation.setShiyongLiushui(sdf.format(date)+seqVal);
            packageImformation.setShiyongkeshi("15");
            packageImformation.setPatientId(shiyong.getPatientName());
            packageImformation.setPatientHospitalNo(shiyong.getHosId());
            packageImformation.setShoushuTemplate(shiyong.getOperationName());
            packageImformation.setShiyongTime(date);
            List<ZsWupinShiyong> shiyongs  = new ArrayList<>();
            List<ZsPackageImformation> imformations = new ArrayList<>();
            imformations.add(packageImformation);
            shiyongs.add(wupinShiyong);
            if (!shiyongs.isEmpty()) {
                wupinshiyongService.insertBatch(shiyongs);
            }
            if (!imformations.isEmpty()) {
                imformationService.updateByBatch(imformations);
            }
            rtnMap.put("status", true);
            rtnMap.put("msg","接收成功");
        }catch (Exception e){

            rtnMap.put("status", false);
            rtnMap.put("msg", "保存成功");
            e.printStackTrace();
        }

        return rtnMap;
    }

    static  class  PackageAndroidYoutiaoma{
        String keshiId;

        public String getKeshiId() {
            return keshiId;
        }

        public void setKeshiId(String keshiId) {
            this.keshiId = keshiId;
        }

        //条码
        String packageTiaoma;
        //包名
        String packageName;
        //部门
        String depName;
        //包装类型
        String baoZhuangName;
        //灭菌方式
        String miejunFangshi;

        public String getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }

        public String getDepName() {
            return depName;
        }

        public void setDepName(String depName) {
            this.depName = depName;
        }

        public String getBaoZhuangName() {
            return baoZhuangName;
        }

        public void setBaoZhuangName(String baoZhuangName) {
            this.baoZhuangName = baoZhuangName;
        }

        public String getMiejunFangshi() {
            return miejunFangshi;
        }

        public void setMiejunFangshi(String miejunFangshi) {
            this.miejunFangshi = miejunFangshi;
        }

        public String getPackageTiaoma() {

            return packageTiaoma;
        }

        public void setPackageTiaoma(String packageTiaoma) {
            this.packageTiaoma = packageTiaoma;
        }
    }


    static  class Shiyong{
        //手术单号
        String applyId;
        //病人姓名
        String patientName;
        //性别
        String patientSex;
        //使用时间
        String useTime;
        //住院号
        String hosId;
        //手术名字
        String operationName;
        //条码
        String packNo;
        //手术室编号
        String roomId;

        public String getApplyId() {
            return applyId;
        }

        public void setApplyId(String applyId) {
            this.applyId = applyId;
        }

        public String getPatientSex() {
            return patientSex;
        }

        public void setPatientSex(String patientSex) {
            this.patientSex = patientSex;
        }

        public String getUseTime() {
            return useTime;
        }

        public void setUseTime(String useTime) {
            this.useTime = useTime;
        }

        public String getHosId() {
            return hosId;
        }

        public void setHosId(String hosId) {
            this.hosId = hosId;
        }

        public String getOperationName() {
            return operationName;
        }

        public void setOperationName(String operationName) {
            this.operationName = operationName;
        }

        public String getPackNo() {
            return packNo;
        }

        public void setPackNo(String packNo) {
            this.packNo = packNo;
        }

        public String getRoomId() {
            return roomId;
        }

        public void setRoomId(String roomId) {
            this.roomId = roomId;
        }

        public String getPatientName() {

            return patientName;
        }

        public void setPatientName(String patientName) {
            this.patientName = patientName;
        }


    }






}
